import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import px2rem from "postcss-pxtorem";
import { createHtmlPlugin } from "vite-plugin-html";
const postcss = px2rem({
  rootValue: 100, // UI设计稿的宽度/10
  propList: ["*"], // 需要转换的属性 *表示所有
  selectorBlackList: [".logo-container"], // 不进行px转换的选择器
  // unitPrecision: 3, // 转rem精确到小数点多少位
  // replace: true, // 是否直接更换属性值，而不添加备用属性
  // mediaQuery: false, // 是否在媒体查询的css代码中也进行转换
  // minPixelValue: 0, // 设置要替换的最小像素值
  // exclude: /node_modules/i // 排除node_modules文件夹下的文件
});
const getViteEnv = (mode, target) => {
  return loadEnv(mode, process.cwd())[target];
};

export default ({ mode }) =>
  // https://vitejs.
  defineConfig({
    plugins: [
      vue(),
      createHtmlPlugin({
        inject: {
          data: {
            // logo: getViteEnv(mode, "VITE_APP_LOGO"),
            title: getViteEnv(mode, "VITE_APP_TITLE"),
          },
        },
      }),
    ],
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"), // 设置 `@` 指向 `src` 目录
      },
      // 忽略后缀名的配置选项, 添加 .vue 选项时要记得原本默认忽略的选项也要手动写入
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    },
    server: {
      open: true,
      port: 5312,
      host: "0.0.0.0", // 配置项目可以局域网访问
      proxy: {
        "/algo/": {
          target: "http://122.9.78.91:30000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/algo\//, "/algo/"),
        },
        "^/order/": {
          target: "http://122.9.78.91:30000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/order\//, "/order/"),
        },
        "/eco": {
          target: "http://122.9.78.91:30000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/eco/, "/eco"),
        },
        "/ams": {
          target: "http://122.9.78.91:30000",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/ams/, "/ams"),
        },
        "/aiShopAssets": {
          target: "http://dev.aicenter.defaulttenant.lcap.visb.cloud:50080",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/aiShopAssets/, ""),
        },
      },
    },
    css: {
      postcss: {
        plugins: [postcss],
      },
      preprocessorOptions: {
        scss: {
          additionalData:
            '@use "./src/styles/variables.scss" as *;@import "./src/styles/mixins.scss";@import "./src/styles/function.scss";', //设置全局样式
        },
      },
    },
  });
